use actix_files::Files;
use actix_web::{middleware, web, App, HttpServer};
use std::env;
mod config;
mod log_info;
mod monitor;

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let log = format!("{}/log.toml", &env::current_dir().unwrap().display());
    log4rs::init_file(&log, Default::default()).unwrap();

    let binding_address = format!("0.0.0.0:{}", config::CONFIG.port());

    //monitor::FILE_MONITOR.start();

    HttpServer::new(|| {
        App::new()
            //启用日志
            .wrap(middleware::Logger::default())
            //跨域和缓存处理
            .wrap(middleware::DefaultHeaders::new().add(("Access-Control-Allow-Origin", "*")))
            .wrap(middleware::DefaultHeaders::new().add(("Cache-Control", "public, max-age=0")))
            //查询日志
            .service(web::resource("/log_query").route(web::post().to(log_info::list_logs)))
            //从access.log生成文档到mongo
            .service(web::resource("/log_load").route(web::post().to(log_info::load_log)))
            //获取日志文件列表接口
            .service(web::resource("/log_options").route(web::post().to(log_info::list_log_options)))
            //列出指定目录下文件和目录
            .service(web::resource("/list_dir").route(web::post().to(log_info::list_dir)))
            //获取左侧菜单
            .service(web::resource("/list_menu").route(web::post().to(log_info::list_menu)))
            //日志下载
            .service(web::resource("/log_down").route(web::post().to(log_info::log_download)))
            //日志导出
            .service(web::resource("/log_export").route(web::post().to(log_info::log_export)))
            //服务端日志选择下拉框获取
            .service(web::resource("/list_service_log").route(web::post().to(log_info::list_service_log)))
            //前端异常上报
            .service(web::resource("/append_front_trace").route(web::get().to(log_info::append_front_trace)))
            //前端异常信息展示
            .service(web::resource("/list_front_trace").route(web::post().to(log_info::list_front_trace)))
            //后端错误上报
            .service(web::resource("/append_service_error").route(web::get().to(log_info::append_service_error)))
            //后端错误信息展示
            .service(web::resource("/list_service_error").route(web::post().to(log_info::list_service_error)))
            //后端错误信息展示
            //.service(web::resource("/get_proc_param").route(web::post().to(log_info::get_proc_param)))
            //获取SQL语句
            .service(web::resource("/extract_sql").route(web::post().to(log_info::extract_sql)))
            //获取SQL语句
            .service(web::resource("/append_front_report").route(web::get().to(log_info::append_front_report)))
            //获取SQL语句
            .service(web::resource("/list_front_report").route(web::post().to(log_info::list_front_report)))
            //起始页
            .service(Files::new("/", "./static/").index_file("index.html"))
    })
    .bind(binding_address)?
    .run()
    .await
}
